package com.keepers.repair.helper;

import android.media.MediaPlayer;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.FileIOUtils;
import com.keepers.repair.net.DataLoader;
import com.keepers.repair.utils.AlertToast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * auth aboom
 * date 2018/10/29
 */
public class MediaPlayHelper {

    private List<PlayInfo> playFilePaths = new ArrayList<>();
    private String lastMedia;

    private MediaPlayer    mediaPlayer;
    private onPlayListener onPlayListener;

    public MediaPlayHelper() {
    }

    public void down(String userName, String name) {
        if (TextUtils.isEmpty(name)) {
            return;
        }
        DataLoader.downloadChatVoice(name, body -> {
            if (body == null) {
                AlertToast.show("语音下载出错...");
                return;
            }
            File sampleDir = new File(Environment.getExternalStorageDirectory()
                    + File.separator + ".repaired/voice/" + name);//录制视频的保存地址
            boolean b = FileIOUtils.writeFileFromIS(sampleDir, body.byteStream());
            if (b) {
                play(userName, sampleDir.toString());
            }
        });
    }

    private void play(String userName, String soundFilePath) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnCompletionListener(mp -> {
                lastMedia = null;
                if (onPlayListener != null) {
                    onPlayListener.onPlay(null, null);
                }
                playAndSetData();
            });
        }
        Log.e("aa", " soundFilePath= " + soundFilePath);
        PlayInfo playInfo = new PlayInfo(userName, soundFilePath);
        playFilePaths.add(playInfo);
        if (!mediaPlayer.isPlaying() && lastMedia == null) {
            playAndSetData();
        }
//        try {
//            mediaPlayer.setDataSource(soundFilePath);
//            mediaPlayer.prepareAsync();
//            mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        mediaPlayer.setOnCompletionListener(mp -> {
//            mediaPlayer.reset();
//            if (CollectionUtil.isNotEmpty(playFilePaths)) {
//                String path = playFilePaths.remove(0);
//                play(path);
//            } else {
//                mediaPlayer.stop();
//                mediaPlayer.release();
//                mediaPlayer = null;
//            }
//        });
    }


    private void playAndSetData() {
        if (playFilePaths.isEmpty()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
            return;
        }
        try {
            mediaPlayer.reset();
            PlayInfo remove = playFilePaths.remove(0);
            Log.e("aa", "play path= " + remove.path);
            mediaPlayer.setDataSource(remove.path);
            lastMedia = remove.path;
            mediaPlayer.prepare();
            mediaPlayer.start();
            if (onPlayListener != null) {
                onPlayListener.onPlay(remove.userName, remove.path);
            }
        } catch (IOException e) {
            e.printStackTrace();
            lastMedia = null;
        }
    }


    public interface onPlayListener {
        void onPlay(String userName, String playPath);
    }

    public void setOnPlayListener(onPlayListener onPlayListener) {
        this.onPlayListener = onPlayListener;
    }


    public static class PlayInfo {
        public String userName;
        public String path;

        public PlayInfo(String userName, String path) {
            this.userName = userName;
            this.path = path;
        }
    }
}
